package com.ztewatch.wearcontacts;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class PasswordkeypadActivity extends Activity {
	public static final String NEW_CONTACT_ACTION = "new_contact_action";
	public static final String EDIT_CONTACT_ACTION = "edit_contact_action";
	public static final String SORT_CONTACT_ACTION = "sort_contact_action";
	public static final String FOLDER_CONTACT_ACTION = "folder_contact_action";
	private Button digitkeypad_1;
	private Button digitkeypad_2;
	private Button digitkeypad_3;
	private Button digitkeypad_4;
	private Button digitkeypad_5;
	private Button digitkeypad_6;
	private Button digitkeypad_7;
	private Button digitkeypad_8;
	private Button digitkeypad_9;
	private Button digitkeypad_0;
	private Button digitkeypad_c;
	private ImageView digitkeypad_delete;
	private EditText digitkeypad_edittext;
	private String digitnum = "";
	private int length = 4;
	private boolean group_detail_flag;
	private boolean contact_detail_flag;
	private boolean voice_call_flag;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_passwordkeypad);
		initIntent();
		initView();
	}

	private void initIntent() {
		group_detail_flag = getIntent().getBooleanExtra(
				WearContactGroupDetailActivity.GROUP_DETAIL_FLAG, false);
		contact_detail_flag = getIntent().getBooleanExtra(
				WearContactDisplayActivity.CONTACT_DETAIL_FALG, false);
		voice_call_flag = getIntent().getBooleanExtra(WearContactVoiceCallMainActivity.VOICE_CALL_FLAG, false);
	}

	private void initView() {
		digitkeypad_1 = (Button) findViewById(R.id.digitkeypad_1);
		digitkeypad_2 = (Button) findViewById(R.id.digitkeypad_2);
		digitkeypad_3 = (Button) findViewById(R.id.digitkeypad_3);
		digitkeypad_4 = (Button) findViewById(R.id.digitkeypad_4);
		digitkeypad_5 = (Button) findViewById(R.id.digitkeypad_5);
		digitkeypad_6 = (Button) findViewById(R.id.digitkeypad_6);
		digitkeypad_7 = (Button) findViewById(R.id.digitkeypad_7);
		digitkeypad_8 = (Button) findViewById(R.id.digitkeypad_8);
		digitkeypad_9 = (Button) findViewById(R.id.digitkeypad_9);
		digitkeypad_0 = (Button) findViewById(R.id.digitkeypad_0);
		digitkeypad_c = (Button) findViewById(R.id.digitkeypad_c);
		digitkeypad_delete = (ImageView) findViewById(R.id.digitkeypad_delete);
		digitkeypad_edittext = (EditText) findViewById(R.id.digitpadedittext);
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(digitkeypad_edittext.getWindowToken(), 0);
		digitkeypad_edittext.addTextChangedListener(new MyTextWatcher());
		digitkeypad_edittext.clearFocus();

		DigitPasswordKeypadOnClickListener dkol = new DigitPasswordKeypadOnClickListener();
		digitkeypad_1.setOnClickListener(dkol);
		digitkeypad_2.setOnClickListener(dkol);
		digitkeypad_3.setOnClickListener(dkol);
		digitkeypad_4.setOnClickListener(dkol);
		digitkeypad_5.setOnClickListener(dkol);
		digitkeypad_6.setOnClickListener(dkol);
		digitkeypad_7.setOnClickListener(dkol);
		digitkeypad_8.setOnClickListener(dkol);
		digitkeypad_9.setOnClickListener(dkol);
		digitkeypad_0.setOnClickListener(dkol);
		digitkeypad_c.setOnClickListener(dkol);
		digitkeypad_delete.setOnClickListener(dkol);
	}

	public void initInputLable(String str, int length) {
		str = str.trim();
		digitnum = str;
		this.length = length;
		digitkeypad_edittext.setText(digitnum);
		digitkeypad_edittext.setSelection(digitnum.length());
	}

	private void startOtherActivityWithAction() {
		if (group_detail_flag) {
			if (NEW_CONTACT_ACTION.equals(getIntent().getAction())) {
				long groupId = getIntent().getLongExtra(
						WearContactGroupDetailActivity.GROUP_DETAIL_ID, 0);
				startOtherActivityWithActionHelp(WearContactEditActivity.class,
						groupId);
			} else if (EDIT_CONTACT_ACTION.equals(getIntent().getAction())) {
				startOtherActivityWithActionHelp(
						WearContactGroupDetailEditActivity.class, getIntent()
								.getData());
			} else if (SORT_CONTACT_ACTION.equals(getIntent().getAction())) {
				startOtherActivityWithActionHelp(
						WearContactGroupDetailSortActivity.class, getIntent()
								.getData());
			}
		} else if (contact_detail_flag) {
			startOtherActivityWithActionHelp(WearContactEditActivity.class,
					getIntent().getData());
		} else {
			if (NEW_CONTACT_ACTION.equals(getIntent().getAction())) {
				startOtherActivityWithActionHelp(WearContactEditActivity.class,
						null);
			} else if (EDIT_CONTACT_ACTION.equals(getIntent().getAction())) {
				startOtherActivityWithActionHelp(
						WearContactEditMainActivity.class, getIntent()
								.getData());
			} else if (SORT_CONTACT_ACTION.equals(getIntent().getAction())) {
				startOtherActivityWithActionHelp(WearContactSortActivity.class,
						getIntent().getData());
			} else if(FOLDER_CONTACT_ACTION.equals(getIntent().getAction())){
				startOtherActivityWithActionHelp(WearContactGroupListActivity.class,
						getIntent().getData());
			}
		}
	}

	private void startOtherActivityWithActionHelp(Class<?> cls, Uri uri) {
		Intent intent = new Intent();
		// intent.setAction(PasswordkeypadActivity.EDIT_CONTACT_ACTION);
		intent.setClass(PasswordkeypadActivity.this, cls);
		if (uri != null) {
			intent.setData(uri);
		}
		intent.putExtra(WearContactVoiceCallMainActivity.VOICE_CALL_FLAG, voice_call_flag);
		startActivity(intent);
		finish();
	}

	private void startOtherActivityWithActionHelp(Class<?> cls, long groupId) {
		Intent intent = new Intent();
		// intent.setAction(PasswordkeypadActivity.EDIT_CONTACT_ACTION);
		intent.setClass(PasswordkeypadActivity.this, cls);
		if (groupId != 0) {
			intent.putExtra(WearContactGroupDetailActivity.GROUP_DETAIL_ID,
					groupId);
		}
		startActivity(intent);
		finish();
	}

	private class DigitPasswordKeypadOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			int viewId = v.getId();

			switch (viewId) {
			case R.id.digitkeypad_1:
				if (digitnum.length() == length) {
					return;
				} else {
					digitnum += 1;
				}
				break;
			case R.id.digitkeypad_2:
				if (digitnum.length() == length) {
					return;
				} else {
					digitnum += 2;
				}
				break;
			case R.id.digitkeypad_3:
				if (digitnum.length() == length) {
					return;
				} else {
					digitnum += 3;
				}
				break;
			case R.id.digitkeypad_4:
				if (digitnum.length() == length) {
					return;
				} else {
					digitnum += 4;
				}
				break;
			case R.id.digitkeypad_5:
				if (digitnum.length() == length) {
					return;
				} else {
					digitnum += 5;
				}
				break;
			case R.id.digitkeypad_6:
				if (digitnum.length() == length) {
					return;
				} else {
					digitnum += 6;
				}
				break;
			case R.id.digitkeypad_7:
				if (digitnum.length() == length) {
					return;
				} else {
					digitnum += 7;
				}
				break;
			case R.id.digitkeypad_8:
				if (digitnum.length() == length) {
					return;
				} else {
					digitnum += 8;
				}
				break;
			case R.id.digitkeypad_9:
				if (digitnum.length() == length) {
					return;
				} else {
					digitnum += 9;
				}
				break;
			case R.id.digitkeypad_0:
				if (digitnum.length() == length) {
					return;
				} else {
					digitnum += 0;
				}

				break;
			case R.id.digitkeypad_delete:
				if (digitnum.length() > 0) {
					digitnum = digitnum.substring(0, digitnum.length() - 1);
				}
				break;
			}
			digitkeypad_edittext.setText(digitnum);
			digitkeypad_edittext.setSelection(null != digitnum ? digitnum
					.length() : 0);
		}

	}

	private class MyTextWatcher implements TextWatcher {

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			if ("1234".equals(s.toString())) {
				startOtherActivityWithAction();
			}
		}

		@Override
		public void afterTextChanged(Editable s) {

		}

	}
}
